<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\api\controllers;

use yii\web\Controller;
use app\models\GkyQyhSpLeave;
use app\api\Response;
use app\api\ApprovalMessage;

/**
 * Description of LeaveController
 *
 * @author yuilin
 */
class LeaveController extends Controller {

    public $enableCsrfValidation = false;

    //put your code here
    public function actionCreate($userId) {
        $postData = \Yii::$app->request->post();
        $model = new GkyQyhSpLeave();
        $model->proposer_id = $userId;
        $model->approver_id = isset($postData['approvaler']) ? $postData['approvaler'] : 1;
        $model->department = isset($postData['department']) ? $postData['department'] : '1';
        $model->business_place = isset($postData['businessPlace']) ? $postData['businessPlace'] : '1';
        $model->go_time = isset($postData['goTime']) ? $postData['goTime'] : '0000-00-00 00:00:00';
        $model->back_time = isset($postData['backTime']) ? $postData['backTime'] : '0000-00-00 00:00:00';
        $model->create_time = date('Y-m-d H:i:s', time());
        $model->reason = isset($postData['reason']) ? $postData['reason'] : '1';
        $model->print_code = 'LEAV' . rand(1000, 9999) . time();
        $model->status = 0;
        if ($model->save()) {
            Response::setHeader();
            echo 100;
            ApprovalMessage::sendMessage($model->proposer_id, $model->approver_id, 0, '请假');
        } else {
            Response::setHeader();
            echo -100;
        }
    }

    public function actionGetLeave($printCode) {
        $result = GkyQyhSpLeave::getLeave($printCode);
        Response::setHeader();
        echo json_encode($result);
    }

    public function actionAgree($printCode,$opinion) {
        if (GkyQyhSpLeave::agreeLeave($printCode,$opinion)) {
            Response::setHeader();
            echo 1;
            $model = GkyQyhSpLeave::findOne(['print_code' => $printCode]);
            if (\Yii::$app->request->isGet) {
                ApprovalMessage::sendMessage($model->approver_id, $model->proposer_id, 1, '请假', $printCode);
            }
        } else {
            Response::setHeader();
            echo -1;
        }
    }

    public function actionRefuse($printCode,$opinion) {
        if (GkyQyhSpLeave::refuseLeave($printCode,$opinion)) {
            Response::setHeader();
            echo 1;
            $model = GkyQyhSpLeave::findOne(['print_code' => $printCode]);
            if (\Yii::$app->request->isGet) {
                ApprovalMessage::sendMessage($model->approver_id, $model->proposer_id, -1, '请假');
            }
        } else {
            Response::setHeader();
            echo -1;
        }
    }

}
